package com.geeklan.smart.ui.feed.fragment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.geeklan.smart.R;
import com.geeklan.smart.common.Constants;
import com.geeklan.smart.data.model.FeedingRecord;
import com.geeklan.smart.databinding.AdapterFeedingRecordItemBinding;
import com.geeklan.smart.ui.device.VideoPlayActivity;
import com.geeklan.smart.utils.DateUtils;
import com.geeklan.smart.utils.GlideUtils;
import com.geeklan.smart.utils.VideoThumbnailLoader;

import java.util.List;

public class FeedingRecordsAdapter extends RecyclerView.Adapter<FeedingRecordsAdapter.ItemViewHolder> {
    private Context mContext;
    private List<FeedingRecord> itemList;
    private ItemOnClickListener itemOnClickListener;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public FeedingRecordsAdapter(Context context, List<FeedingRecord> itemList) {
        this.itemList = itemList;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FeedingRecordsAdapter.ItemViewHolder(AdapterFeedingRecordItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        FeedingRecord feedingRecord = itemList.get(position);
        holder.mBinding.tvTime.setText(DateUtils.formatTime(feedingRecord.getCreateTime()));
        if(TextUtils.isEmpty(feedingRecord.getImgUrl())){
//            holder.mBinding.ivVideo.setVisibility(View.GONE);
        }else {
            holder.mBinding.ivVideo.setVisibility(View.VISIBLE);
            GlideUtils.loadRoundedImageToUrl(mContext, feedingRecord.getImgUrl(), holder.mBinding.ivVideo);
        }
//        if (feedingRecord.getRecordType() == 1) {//出粮
            holder.mBinding.ivAutoFeed.setVisibility(View.VISIBLE);
            holder.mBinding.ivAutoFeed.setImageResource(feedingRecord.getFeedMethod() == 2 ? R.drawable.ic_auto_feeding : R.drawable.ic_feeding_manual);
//        } else if (feedingRecord.getRecordType() == 2) {//进食
//            holder.mBinding.ivAutoFeed.setVisibility(View.GONE);
//        } else if (feedingRecord.getRecordType() == 3) {//抓拍
//            holder.mBinding.ivAutoFeed.setVisibility(View.GONE);
//        }

        holder.mBinding.tvStatus.setText(feedingRecord.getFeedMethod() == 2 ? "自动喂食" : "手动喂食");
        String content = "";
        if (feedingRecord.getFeedType() == 0) {
            String string = mContext.getString(R.string.feeding_records_number_2);
            content = String.format(string, mContext.getString(R.string.out_food), feedingRecord.getFeedNum());
        } else {
            String string = mContext.getString(R.string.feeding_records_number_1);
            content = String.format(string, mContext.getString(R.string.out_food), feedingRecord.getFeedNum(), Constants.FOOD_WEIGHT * feedingRecord.getFeedNum());
        }
        holder.mBinding.tvFeedPlanMsg.setText(content);

        holder.mBinding.ivVideo.setOnClickListener(v -> {
            if(TextUtils.isEmpty(feedingRecord.getImgUrl())){
                Toast.makeText(mContext,"视频拍摄失败",Toast.LENGTH_SHORT).show();
                return;
            }
            VideoPlayActivity.startActivity(mContext,feedingRecord.getVideoUrl());
        });
//        holder.mBinding.getRoot().setOnClickListener(v -> {
//            itemOnClickListener.OnItemClick(v, position);
//        });
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        AdapterFeedingRecordItemBinding mBinding;

        public ItemViewHolder(@NonNull AdapterFeedingRecordItemBinding itemView) {
            super(itemView.getRoot());
            mBinding = itemView;
        }
    }

    public interface ItemOnClickListener {
        void OnItemClick(View view, int position);
    }
}